package com.bilin.coin.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.tools.Poster;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = "/safe/AlertPassActivity")
public class AlertPassActivity extends BaseAppCompatActivity {

    @BindView(R.id.et_old_pass)
    EditText et_old_pass;
    @BindView(R.id.et_pass_new)
    EditText et_pass_new;
    @BindView(R.id.et_pass_again)
    EditText et_pass_again;

    @Autowired
    int type; //0修改登录密码 1修改交易密码

    @Override
    protected void initView() {
        if (type == 1) {
            et_old_pass.setHint("请输入旧密码");
            et_pass_new.setHint("请输入新密码");
            et_pass_again.setHint("确认新密码");
            setTitle("修改交易密码");
        } else {
            setTitle("修改登录密码");
        }
    }

    @OnClick({R.id.bt_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:

                alert();
                break;
        }
    }

    private void alert() {
        //int type; //0修改登录密码 1修改交易密码
        String newPass = et_pass_new.getText().toString().trim();
        String newPassAgain = et_pass_again.getText().toString().trim();
        String oldPass = et_old_pass.getText().toString().trim();
        if (TextUtils.isEmpty(oldPass)) {
            showToast("旧密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(newPass)) {
            showToast("新密码不能为空");
            return;
        }
        if (!newPass.equals(newPassAgain)) {
            showToast("两次密码不一致");
            return;
        }

        new Poster(this, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/user/changePwd";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("newPwd",newPass);
                hashMap.put("pwd",oldPass);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.LoginPassAlertSuccess));
                finish();
            }
        };
    }


    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_alert_pass;
    }
}
